<template>
  <view class="page-container confirm-order-container">
    <!-- 地址和服务时间选择 -->
    <view
      class="card-box"
      v-if="goods.goods_type == 1 || goods.goods_type == 0 || goods.goods_type == 2"
    >
      <view class="p-20 order-address" @click="gotoAddressPage">
        <view class="order-address-icon">
          <image :src="$img_path('/home/order/icon-address.png')" mode="widthFix"></image>
        </view>
        <view class="order-address-info">
          <view class="order-address-info-box" v-if="address.id">
            <view class="order-address-info-city">
              <text>{{ address.formatted_address }}</text>
            </view>
            <view class="order-address-info-area">
              <text>{{ address.address }}</text>
            </view>
            <view class="order-address-info-user">
              <text>{{ address.name }}</text>
              <text v-if="address.mobile">（{{ address.mobile }}）</text>
            </view>
          </view>
          <view class="order-address-info-box" v-else>
            <text>请选择</text>
          </view>
          <view class="order-address-info-right">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
      <view class="gap" v-if="goods.goods_type == 0 || goods.goods_type == 1"></view>
      <view
        class="p-20 order-service-time"
        @click="gotoSelectServiceTime"
        v-if="goods.goods_type == 0 || goods.goods_type == 1"
      >
        <view class="order-service-time-icon icon-40">
          <image :src="$img_path('/home/order/icon-time.png')" mode="widthFix"></image>
        </view>
        <view class="order-service-time-box">
          <view class="order-service-time-label">
            <text>服务时间</text>
          </view>
          <view class="order-service-time-right">
            <text v-if="serviceTime.time && goods.goods_type != 0"
              >{{ serviceTime.type == "week" ? "本周" : "本月" }}
              {{ serviceTime.values.join(",") }} {{ serviceTime.time }}</text
            >
            <text v-else-if="goods.goods_type == 0 && serviceTime2.date.date">
              {{ serviceTime2.date.date }} {{ serviceTime2.date.weekday }}
              {{ serviceTime2.time.time }}
            </text>
            <text v-else>请选择</text>
            <view class="icon-32">
              <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
            </view>
          </view>
        </view>
      </view>
    </view>
    <!-- 被服务者信息 -->
    <view
      class="to-service-user-info-box card-box p-20 mt-20"
      v-if="(goods.goods_type == 1 && goods.is_yh == 1) || goods.goods_type == 0"
    >
      <view class="to-service-user-title">
        <text>被服务者信息</text>
      </view>
      <view class="to-service-user-select" v-if="!serviceBeUser.id" @click="gotoBeServiceUserList">
        <view class="to-service-user-select-icon">
          <image :src="$img_path('/home/order/icon-add.png')" mode="widthFix"></image>
        </view>
        <view class="to-service-user-select-text">
          <text>选择被服务者信息</text>
        </view>
      </view>
      <view class="to-service-user-select-info" v-else @click="gotoBeServiceUserList">
        <view class="select-user-info">
          <view class="">
            <text
              >{{ serviceBeUser.name }}
              {{ serviceBeUser.sex == 1 ? "男" : "女" }}
              {{ serviceBeUser.gx }}</text
            >
          </view>
          <view class="mobile">
            <text>{{ serviceBeUser.mobile }}</text>
          </view>
        </view>
        <view class="right-icon">
          <view class="icon-32">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
    </view>
    <!-- 性别选择 -->
    <view class="sex-select-box card-box p-20 mt-20" v-if="goods.goods_type == 0">
      <view class="to-service-user-title">
        <text>性别</text>
      </view>
      <view class="sex-select-sexs">
        <view class="sex-item" @click="selectSex('')">
          <view class="sex-icon">
            <image
              v-if="orderFormData.sex == ''"
              :src="$img_path('/course/pay_checkbox1.png')"
              mode="widthFix"
            ></image>
            <image v-else :src="$img_path('/course/pay_checkbox2.png')" mode="widthFix"></image>
          </view>
          <text>不限</text>
        </view>
        <view class="sex-item" @click="selectSex(1)">
          <view class="sex-icon">
            <image
              v-if="orderFormData.sex == 1"
              :src="$img_path('/course/pay_checkbox1.png')"
              mode="widthFix"
            ></image>
            <image v-else :src="$img_path('/course/pay_checkbox2.png')" mode="widthFix"></image>
          </view>
          <text>男</text>
        </view>
        <view class="sex-item" @click="selectSex(2)">
          <view class="sex-icon">
            <image
              v-if="orderFormData.sex == 2"
              :src="$img_path('/course/pay_checkbox1.png')"
              mode="widthFix"
            ></image>
            <image v-else :src="$img_path('/course/pay_checkbox2.png')" mode="widthFix"></image>
          </view>
          <text>女</text>
        </view>
      </view>
    </view>
    <!-- 服务人员 -->
    <view
      class="service-select-box mt-20 card-box"
      @click="gotoSelectServiceUserList"
      v-if="goods.goods_type == 1"
    >
      <view class="icon-40">
        <image :src="$img_path('/home/order/icon-aixin.png')" mode="widthFix"></image>
      </view>
      <view class="service-select-right">
        <view class="">
          <text>服务者选择</text>
        </view>
        <view class="display-flex alig">
          <text v-if="serviceUser.id">{{ serviceUser.name }}</text>
          <text v-else>请选择</text>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
    </view>
    <!-- 服务商品信息 -->
    <view class="card-box mt-20 service-goods-info-box">
      <view class="service-goods-info-item">
        <view class="service-goods-info-item-image">
          <lazy-image :src="goods.image"></lazy-image>
        </view>
        <view class="service-goods-info-item-right">
          <view class="goods-info">
            <text>{{ goods.name }}</text>
            <text class="goods-info-price"
              ><text class="goods-info-price-pre">￥</text
              >{{ sku.id ? sku.price : goods.price }}</text
            >
          </view>
          <view class="goods-sku" v-if="sku.id">
            <text>{{ sku.name }}</text>
          </view>
          <!-- <view class="goods-sku display-flex space-between alig">
						<text>规格名称规格名称10次</text>
						<text>x1</text>
					</view> -->
        </view>
      </view>
      <view class="gap"></view>
      <view class="row-b service-goods-row-item" @click="gotoBoundDetail">
        <view class="label">
          <text>保险介绍</text>
          <view class="" style="font-size: 28rpx; margin-top: 6rpx">
            <text v-if="goods.bx_desc && goods.bx_desc.length > 10"
              >{{ goods.bx_desc.split(0, 10)[0] }}...</text
            >
            <text v-else>{{ goods.bx_desc }}</text>
          </view>
        </view>
        <view class="flex items-center">
          <view class="sub-label" style="color: #999999"> 免费赠送 </view>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
      <template v-if="goods.goods_type == 0">
        <view class="gap" v-if="goodsaddListTwo.length > 0"></view>
        <view class="more-service-box-item" v-if="goodsaddListTwo.length > 0">
          <view class="title">
            <text>附加服务</text>
          </view>
          <view class="more-swiper-box mt-20">
            <scroll-view class="more-swiper_H" scroll-x="true">
              <view
                class="more-swiper-item_H"
                v-for="(item, index) in goodsaddListTwo"
                :key="index"
              >
                <moerServiceItemVue :addGoods="item" @selectAddGoods="selectAddGoods" />
              </view>
            </scroll-view>
          </view>
        </view>
        <view class="gap"></view>
        <view class="row-b service-goods-row-item">
          <view class="label">
            <text>总费用</text>
          </view>
          <view class="flex items-center">
            <view class="sub-label"> ¥ {{ totalAmount }} </view>
          </view>
        </view>
        <view class="gap" v-if="serviceTime2.time.isNight"></view>
        <view class="row-b service-goods-row-item" v-if="serviceTime2.time.isNight">
          <view class="label">
            <text>夜间费</text>
          </view>
          <view class="flex items-center">
            <view class="sub-label"> ¥ {{ goods.night_money }} </view>
          </view>
        </view>
      </template>
      <view class="gap"></view>
      <view class="row-b service-goods-row-item" @click="openCouponPopup">
        <view class="label">
          <text>优惠券</text>
        </view>
        <view class="flex items-center">
          <view class="sub-label" v-if="coupon.id"> -¥ {{ coupon.reduce }} </view>
          <view class="sub-label" v-else>
            <text>请选择</text>
          </view>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
      <view class="gap" v-if="goods.goods_type != 0"></view>
      <view class="row-b service-goods-row-item" v-if="goods.goods_type != 0">
        <view class="label">
          <text>备注</text>
        </view>
        <view class="flex items-center" @click="openRemark">
          <view class="sub-label">
            {{ orderFormData.memo ? "有备注" : "无备注" }}
          </view>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
    </view>
    <!-- 疾病选择 -->
    <view
      class="service-select-box mt-20 card-box"
      v-if="(goods.is_yh == 1 && goods.goods_type == 1) || goods.goods_type == 0"
      @click="openSelectPopup"
    >
      <view class="icon-40">
        <image :src="$img_path('/home/order/icon-bing.png')" mode="widthFix"></image>
      </view>
      <view class="service-select-right">
        <view class="">
          <text>疾病选择</text>
        </view>
        <view class="display-flex alig">
          <text>{{ orderFormData.case }}</text>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
    </view>
    <!--  -->
    <view
      class="switch-box card-box mt-20 p-20"
      v-if="(goods.is_yh == 1 && goods.goods_type == 1) || goods.goods_type == 0"
    >
      <view class="switch-box-title">
        <text>是否有医护服务名称工具？</text>
      </view>
      <view class="switch-box-btns">
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_yh_tools == 1,
          }"
          @click="switchChange(1)"
        >
          <text>有</text>
        </view>
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_yh_tools == 0,
          }"
          @click="switchChange(0)"
        >
          <text>无</text>
        </view>
      </view>
      <view class="" v-if="orderFormData.is_yh_tools == 1">
        <view
          class="display-flex space-between alig margin-top-30"
          v-for="(item, index) in goodsaddList"
          :key="index"
          @click="gotoServicePackageDetail(item)"
        >
          <view class="display-flex alig">
            <view class="sex-icon" @click.stop="selectGoodsadd(item)">
              <image
                v-if="goodsadd2Ids.includes(item.id)"
                :src="$img_path('/course/pay_checkbox1.png')"
                mode="widthFix"
              >
              </image>
              <image v-else :src="$img_path('/course/pay_checkbox2.png')" mode="widthFix"></image>
            </view>
            <view style="font-weight: bold; font-size: 30rpx; color: #333333">
              <text>{{ item.name }}x1（¥ {{ item.price }}）</text>
            </view>
          </view>
          <view class="icon-24">
            <image :src="$img_path('/home/icon-right.png')" mode="widthFix"></image>
          </view>
        </view>
      </view>
    </view>
    <view
      class="switch-box card-box mt-20 p-20"
      v-if="(goods.is_yh == 1 && goods.goods_type == 1) || goods.goods_type == 0"
    >
      <view class="switch-box-title">
        <text>是否有过敏史？</text>
      </view>
      <view class="switch-box-btns">
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_gm == 1,
          }"
          @click="gmSwitchChange(1)"
        >
          <text>有</text>
        </view>
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_gm == 0,
          }"
          @click="gmSwitchChange(0)"
        >
          <text>无</text>
        </view>
      </view>
      <view class="mt-20" v-if="orderFormData.is_gm == 1">
        <textarea v-model="orderFormData.gm_text" class="switch-textarea" placeholder="请输入" />
      </view>
    </view>
    <view
      class="upload-file-box card-box mt-20 p-20"
      v-if="(goods.is_yh == 1 && goods.goods_type == 1) || goods.goods_type == 0"
    >
      <view class="switch-box-title">
        <text>是否有就医证明？</text>
      </view>
      <view class="switch-box-btns">
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_jyzm == 1,
          }"
          @click="confirmJyzm(1)"
        >
          <text>有</text>
        </view>
        <view
          class="switch-box-btn"
          :class="{
            'common-btn-bg-color switch-box-btn-active': orderFormData.is_jyzm == 0,
          }"
          @click="confirmJyzm(0)"
        >
          <text>无</text>
        </view>
      </view>
      <view class="upload-file-btns" v-if="orderFormData.is_jyzm">
        <view class="upload-file-btn" @click="selectImage('cf_images')">
          <lazy-image v-if="orderFormData.cf_images" :src="orderFormData.cf_images"></lazy-image>
          <image v-else :src="$img_path('/home/order/upload-1.png')" mode="widthFix"></image>
          <!-- <upload-file></upload-file> -->
        </view>
        <view class="upload-file-btn" @click="selectImage('yp_images')">
          <lazy-image v-if="orderFormData.yp_images" :src="orderFormData.yp_images"></lazy-image>
          <image v-else :src="$img_path('/home/order/upload-2.png')" mode="widthFix"></image>
          <!-- <upload-file></upload-file> -->
        </view>
      </view>
    </view>
    <!-- 底部固定栏 -->
    <view class="bottom-box">
      <view class="bottom-all-money">
        <text class="pre">￥</text>
        <text>{{ totalAmount }}</text>
        <text class="sufix">起</text>
      </view>
      <view class="common-btn-bg-color bottom-confirm-btn" @click="submit">
        <text>提交订单</text>
      </view>
    </view>
    <!-- 支付弹窗 -->
    <PayPopup ref="payPopup" :amount="totalAmount" @confirm="payConfrim" />
    <!-- 服务时间弹窗 -->
    <serviceTimePopup
      ref="serviceTimePopup"
      :nightMoney="goods.night_money ? Number(goods.night_money) : 0"
      @confirm="serviceTimeConfirm"
    />
    <!-- 疾病选择弹窗 -->
    <selectPopupVue ref="selectPopup" :list="j_list" @confirm="confirmCase" />

    <!-- 实名提示弹窗 -->
    <service-modal
      ref="realNamePopup"
      title="提示"
      confirm-text="立即实名"
      @confirm="gotoRealNamePage"
    >
      <view
        class="display-flex alig center flex-direction-column"
        style="font-weight: bold; font-size: 32rpx; color: #000000; height: 150rpx"
      >
        <view class="">
          <text>您还未实名认证</text>
        </view>
        <view class="">
          <text>暂无法下单预约</text>
        </view>
      </view>
    </service-modal>

    <!-- 优惠券弹窗 -->
    <couponPopupVue ref="couponPopup" @confirm="confirmCoupon" />

    <!-- 备注弹窗 -->
    <remarkPopupVue ref="remarkPopupRef" @confirm="confirmRemark" />
  </view>
</template>

<script>
import PayPopup from "@/components/pay-popup/pay-popup.vue";
import serviceTimePopup from "./components/service-time-popup/service-time-popup.vue";
import moerServiceItemVue from "./components/moer-service-item/moer-service-item.vue";
import selectPopupVue from "./components/select-popup/select-popup.vue";
import couponPopupVue from "./components/coupon-popup/coupon-popup.vue";
import remarkPopupVue from "./components/remark-popup/remark-popup.vue";

import {
  getServiceGoodsDetail,
  getAddressList,
  getSysConfig,
  getGoodsAddList,
  getGoodsOrderSettle,
  getGoodsOrderCreate,
} from "@/utils/api/serviceApi";
import { upload } from "@/utils/api/public.js";
export default {
  components: {
    PayPopup,
    moerServiceItemVue,
    serviceTimePopup,
    selectPopupVue,
    couponPopupVue,
    remarkPopupVue,
  },
  onLoad(options) {
    console.log("options", options);
    this.goodsId = options.goods_id || 27;
    this.skuId = options.sku_id || "";

    this.getServiceGoodsDetail();
    this.getAddressList();
    this.getJiBingList();
  },
  data() {
    return {
      orderFormData: {
        // 医护工具:0=无,1=有
        is_yh_tools: 1,
        is_gm: 1,
        // 是否有就医证明
        is_jyzm: 1, //0无1有
        case: "",
        // 优惠券ID
        coupon_id: "",
        // 地址ID
        address_id: "",
        // 城市
        city: "",
        // 数量
        num: "",
        // 服务者ID
        skill_id: "",
        // 服务时间
        starttime: "",
        // 备注
        memo: "",
        // 夜间费用
        night_money: 0,
        // 商品ID
        goods_id: "",
        // 规格ID
        goods_sku_id: "",
        // 支付方式:0=微信小程序,1=微信APP,2=公众号支付,3=支付宝APP,4=余额
        paytype: 0,
        // 选择服务人员:0=否,1=是
        choose_skill_type: "",
        // 被服务者ID
        bfw_id: "",
        // 被服务者姓名
        bfw_name: "",
        // 被服务者手机号
        bfw_mobile: "",
        // 被服务者性别
        bfw_sex: "",
        // 服务者性别
        sex: "",
        // 过敏史说明
        gm_text: "",
        // 就医处方照片
        cf_images: "",
        // 就医药品照片
        yp_images: "",
        // 附加服务id
        goodsadd_ids: "",
        // 医疗工具id
        goodsadd2_ids: "",
        // 服务包时间
        service_time_json: "",
        // 委托订单
        is_wt: "", //0否1是
        // 委托说明
        wt_desc: "",
        // 委托图片
        wt_images: "",
      },
      j_list: [],
      sku: {},
      skuId: null,
      goodsId: null,
      goods: {},
      address: {},
      serviceTime: {
        time: "",
        type: "week",
        values: [],
      },
      serviceTime2: {
        date: {
          date: "",
          weekday: "",
        },
        time: {
          time: "",
        },
      },
      serviceBeUser: {},
      serviceUser: {},
      coupon: {},
      goodsaddList: [], //医护工具
      goodsadd2Ids: [],

      goodsaddListTwo: [], //附加项目
      goodsaddListTwoIds: [],

      goodsType: {
        0: "家政商品",
        1: "定死服务",
        2: "按次服务",
      },
      totalAmount: 0,

      // 选择的服务时间（时间戳）
      selectServiceTime: 0,
    };
  },
  methods: {
    switchChange(value) {
      this.orderFormData.is_yh_tools = value;
      if (value == 0) {
        this.goodsadd2Ids = [];
        this.getGoodsOrderSettle();
      }
    },
    gmSwitchChange(value) {
      this.orderFormData.is_gm = value;
    },
    // 是否有就医证明
    confirmJyzm(value) {
      this.orderFormData.is_jyzm = value;
    },
    selectSex(type) {
      this.orderFormData.sex = type;
    },

    gotoAddressPage() {
      const that = this;
      uni.navigateTo({
        url: "/pagesService/address/index",
        success: function () {
          uni.$once("updateServiceOrderAddress", (data) => {
            console.log("updateServiceOrderAddress data", data);
            that.address = data.data;
          });
        },
      });
    },
    // 选择服务时间
    gotoSelectServiceTime() {
      const that = this;
      if (this.goods.goods_type == 0) {
        this.$refs.serviceTimePopup.open();
      } else {
        uni.navigateTo({
          url: "/pagesService/serviceTime/serviceTime",
          success: function () {
            //
            uni.$once("updateServiceTime", (data) => {
              console.log("updateServiceTime data", data);
              that.serviceTime = data.data;
            });
          },
        });
      }
    },
    gotoSelectServiceUserList() {
      const that = this;
      uni.navigateTo({
        url: "/pagesService/serviceUserList/serviceUserList",
        success: function () {
          uni.$once("updateServiceUser", (data) => {
            console.log("updateServiceUser data", data);
            that.serviceUser = data.data;
            that.getGoodsOrderSettle();
          });
        },
      });
    },
    gotoBoundDetail() {
      const str = `?data=${encodeURIComponent(JSON.stringify(this.goods))}`;
      uni.navigateTo({
        url: "/pagesService/boundDetail/boundDetail" + str,
      });
    },
    gotoServicePackageDetail(item) {
      const str = `?data=${encodeURIComponent(JSON.stringify(item))}`;
      uni.navigateTo({
        url: "/pagesService/servicePackageDetail/servicePackageDetail" + str,
      });
    },
    selectGoodsadd(item) {
      // this.orderFormData.goodsadd2_ids = item.id
      if (this.goodsadd2Ids.includes(item.id)) {
        this.goodsadd2Ids = this.goodsadd2Ids.filter((id) => id != item.id);
      } else {
        this.goodsadd2Ids.push(item.id);
      }
      this.getGoodsOrderSettle();
    },
    gotoBeServiceUserList() {
      const that = this;
      uni.navigateTo({
        url: "/pagesService/beServiceUserList/beServiceUserList",
        success: function () {
          uni.$once("updateServiceBeUser", (data) => {
            console.log("updateServiceBeUser data", data);
            that.serviceBeUser = data.data;
          });
        },
      });
    },
    submit() {
      const that = this;
      uni.showModal({
        title: "提示",
        content:
          "您所在订单的位置与您当前所在区域不符，您下单区域的当前的服务价格为XXX元，您是否继续预定？",
        confirmText: "继续预定",
        confirmColor: "#E72A2A",
        cancelColor: "#999999",
        success: function (res) {
          if (res.confirm) {
            that.$refs.payPopup.open();
          }
        },
      });
      // this.$refs.realNamePopup.open()
    },
    openSelectPopup() {
      this.$refs.selectPopup.open(this.orderFormData.case);
    },
    gotoRealNamePage() {
      uni.navigateTo({
        url: "/pagesService/realName/realName",
      });
    },
    openCouponPopup() {
      this.$refs.couponPopup.open(this.coupon);
    },

    confirmCoupon(coupon) {
      this.coupon = coupon;
      this.getGoodsOrderSettle();
    },

    async getServiceGoodsDetail() {
      try {
        const res = await getServiceGoodsDetail({
          id: this.goodsId,
        });
        console.log("商品详情", res);
        this.goods = res.data;
        this.sku = res.data.sku.find((item) => item.id == this.skuId);
        this.getGoodsOrderSettle();
        if (res.data.goods_type == 0 || res.data.is_yh) {
          this.getGoodsAddList();
          this.getGoodsAddListTwo();
        }
      } catch (e) {
        console.log("查询商品详情失败", e);
        //TODO handle the exception
      }
    },

    async getAddressList() {
      try {
        const res = await getAddressList();
        console.log("地址列表", res);
        const { data = [] } = res?.data;
        if (data && data.length) {
          this.address = data.find((item) => item.default == "1") || {};
          console.log("this.address", this.address);
        }
      } catch (e) {
        //TODO handle the exception
        console.log("获取地址列表失败", e);
      }
    },

    openRemark() {
      this.$refs.remarkPopupRef.open(this.orderFormData.memo);
    },

    confirmRemark(data) {
      console.log("data", data);
      this.orderFormData.memo = data.value;
    },

    async getJiBingList() {
      try {
        const res = await getSysConfig({
          site: "case_list",
        });
        console.log("getJiBingList res", res);
        this.j_list = res.data || [];
      } catch (e) {
        //TODO handle the exception
        console.log("getJiBingList err", e);
      }
    },

    confirmCase(value) {
      this.orderFormData.case = value;
    },

    // 医护工具
    async getGoodsAddList() {
      try {
        const res = await getGoodsAddList({
          id: this.goodsId,
          // 0=附加服务,1=医护工具
          type: 1,
        });
        console.log("getGoodsAddList res", res);
        this.goodsaddList = res.data || [];
      } catch (e) {
        //TODO handle the exception
        console.log("getGoodsAddList err", e);
      }
    },
    // 附加工具
    async getGoodsAddListTwo() {
      try {
        const res = await getGoodsAddList({
          id: this.goodsId,
          // 0=附加服务,1=医护工具
          type: 0,
        });
        console.log("getGoodsAddList res", res);
        this.goodsaddListTwo = res.data || [];
      } catch (e) {
        //TODO handle the exception
        console.log("getGoodsAddList err", e);
      }
    },
    // 选择附加服务
    selectAddGoods(data, number) {
      if (number > 0) {
        this.goodsaddListTwoIds.push(data);
      } else {
        let idx = this.goodsaddListTwoIds.indexOf(data);
        this.goodsaddListTwoIds.splice(idx, 1);
      }
      this.getGoodsOrderSettle();
    },

    selectImage(key = "cf_images") {
      const that = this;
      uni.chooseImage({
        count: 1,
        success: (res) => {
          console.log(res);
          const _file = res.tempFiles[0];
          upload({
            file: _file.path,
          }).then((response) => {
            console.log("上传成功", response);
            const url = response.data.url;
            that.orderFormData[key] = url;
          });
        },
      });
      return;
      wx.chooseMessageFile({
        count: 1, //默认100
        type: "image",
        success: function (res) {
          console.log(res);
          const _file = res.tempFiles[0];
          upload({
            file: _file.path,
          }).then((response) => {
            console.log("上传成功", response);
            const url = response.data.url;
            that.orderFormData[key] = url;
          });
        },
      });
    },

    async getGoodsOrderSettle() {
      try {
        const isNight = this.goods.goods_type == 0 ? this.serviceTime2.time.isNight : false;
        const data = {
          goods_id: this.goodsId,
          goods_sku_id: this.skuId,
          num: 1,
          night_money: isNight ? this.goods.night_money : 0,
          skill_id: this.serviceUser.id || "",
          goodsadd_ids: this.goodsaddListTwoIds.join(","),
          goodsadd2_ids: this.goodsadd2Ids.join(","),
          coupon_id: this.coupon.id || "",
        };
        const res = await getGoodsOrderSettle(data);
        if (res.data == "优惠券无法使用") {
          uni.showToast({
            title: res.data,
            icon: "none",
          });
          this.coupon = {};
          this.getGoodsOrderSettle();
          return;
        }
        console.log("getGoodsOrderSettle res", res);
        this.totalAmount = res.data.settle.sumprice;
      } catch (e) {
        //TODO handle the exception
        console.log("getGoodsOrderSettle e", e);
      }
    },

    serviceTimeConfirm(data) {
      console.log("serviceTimeConfirm", data);
      const date = data.date.date;
      const time = data.time.time;
      const year = new Date().getFullYear();
      const fullDate = `${year}-${date.replace(/\-/g, "-")}`;
      const dateTime = `${fullDate} ${time}:00`;
      // const timestamp = new Date(dateTime).getTime() / 1000;//转时间戳
      this.selectServiceTime = dateTime;
      this.serviceTime2 = data;
      this.getGoodsOrderSettle();
    },

    async payConfrim(data) {
      console.log(this.address, "address");
      console.log(this.coupon, "coupon");
      console.log(this.serviceBeUser, "服务者");
      console.log(this.serviceTime, "时间", this.serviceTime2);

      this.orderFormData.address_id = this.address.id; //地址id
      this.orderFormData.goods_id = this.goods.id; //商品id
      this.orderFormData.num = 1; //数量
      this.orderFormData.city = this.address.city; //城市
      this.orderFormData.paytype = data.payType; //支付方式
      this.orderFormData.coupon_id = this.coupon.id || "";
      this.orderFormData.goods_sku_id = this.skuId;
      if (this.goods.goods_type == 0) {
        let year = this.$dayjs().format("YYYY");
        // this.orderFormData.starttime = this.$dayjs(
        //   year + "-" + this.serviceTime2.date.date + " " + this.serviceTime2.time.time
        // ).format("YYYY-MM-DD HH:mm"); //服务时间
        this.orderFormData.starttime = this.selectServiceTime //服务时间
        this.orderFormData.night_money = this.serviceTime2.time.isNight
          ? this.goods.night_money
          : 0;
        this.orderFormData.choose_skill_type = "0"; //选择服务人员:0=否,1=是
        this.orderFormData.bfw_id = this.serviceBeUser.id; //被服务者id
        this.orderFormData.bfw_name = this.serviceBeUser.name; //被服务者姓名
        this.orderFormData.bfw_mobile = this.serviceBeUser.mobile; //被服务者手机号
        this.orderFormData.bfw_sex = this.serviceBeUser.sex; //被服务者性别
        this.orderFormData.case = this.orderFormData.case; //病例
        this.orderFormData.goodsadd2_ids = this.goodsadd2Ids.join(","); //医疗工具
        this.orderFormData.goodsadd_ids = this.goodsaddListTwoIds.join(","); //附加服务
      } else if (this.goods.goods_type == 1) {
        let s_times = [];
        this.serviceTime.values.forEach((e) => {
          s_times.push({
            day: e,
            time: this.serviceTime.time,
          });
        });
        let service_time_json = {
          type: this.serviceTime.type == "week" ? "1" : "2",
          data: s_times,
        };
        this.orderFormData.service_time_json = service_time_json;
        this.orderFormData.choose_skill_type = "1";
        this.orderFormData.skill_id = this.serviceUser.id;
        if (this.goods.is_yh == 1) {
          this.orderFormData.bfw_id = this.serviceBeUser.id; //被服务者id
          this.orderFormData.bfw_name = this.serviceBeUser.name; //被服务者姓名
          this.orderFormData.bfw_mobile = this.serviceBeUser.mobile; //被服务者手机号
          this.orderFormData.bfw_sex = this.serviceBeUser.sex; //被服务者性别
          this.orderFormData.case = this.orderFormData.case; //病例
          this.orderFormData.goodsadd2_ids = this.goodsadd2Ids.join(","); //医疗工具
          this.orderFormData.goodsadd_ids = this.goodsaddListTwoIds.join(","); //附加服务
        }
      } else {
      }
      console.log("data", data, this.orderFormData);
      uni.showLoading({
        title: "支付中...",
      });
      getGoodsOrderCreate(this.orderFormData).then((res) => {
        uni.hideLoading();
        this.$refs.payPopup.close();
        if (res.code == 1) {
          uni.navigateTo({
            url: `/pagesService/servicePayResult/servicePayResult?data=${JSON.stringify(res.data)}`,
          });
        }
      });
    },
  },
};
</script>

<style lang="scss" scoped>
@import "serviceConfrimOrder.scss";
</style>
